package com.sc.javaee.controller;

import com.sc.javaee.pojo.Enterprise;
import com.sc.javaee.pojo.Result;
import com.sc.javaee.service.enterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class enterpriseController {
    @Autowired
    enterpriseService es;
    @RequestMapping("/list")
    public Result getAllEnterprises(){
        List<Enterprise> enterprises=es.getAllEnterprises();
        return new Result(1,"获取成功",enterprises);
    }
    @RequestMapping("/searchEnterprise")
    public Result searchEnterprises(@RequestBody Map<String,String> params){
        String keyword=params.get("keyword");
        List<Enterprise> enterprises=es.searchEnterprises(keyword);
        return new Result(1,"搜索成功",enterprises);
    }
    // 绑定用户到企业
    @RequestMapping("/bindUser")
    public Result bindUserToEnterprise(@RequestBody Map<String, Object> params) {
        Integer userId = (Integer) params.get("userId");
        Integer enterpriseId = (Integer) params.get("enterpriseId");

        boolean success = es.bindUserToEnterprise(userId, enterpriseId);
        if (success) {
            return new Result(1, "绑定成功");
        }
        return new Result(0, "绑定失败");
    }

    // 获取用户绑定的企业
    @RequestMapping("/getUserEnterprise")
    public Result getUserEnterprise(@RequestBody Map<String, Integer> params) {
        Integer userId = params.get("userId");
        Enterprise enterprise = es.getUserEnterprise(userId);
        if (enterprise != null) {
            return new Result(1, "获取成功", enterprise);
        }
        return new Result(0, "未绑定企业");
    }
}
